import { Injectable } from '@angular/core';
import { Platform, ToastController, App, NavController, Keyboard, Tabs, } from 'ionic-angular';
import { TabsPage } from './../../pages/tabs/tabs';
import { HomePage } from "./../../pages/home/home";
import { AboutPage } from "./../../pages/about/about";
import { ShopSearchPage } from "./../../pages/shop-search/shop-search";
import { ContactPage } from "./../../pages/contact/contact";
import { PersonalPage } from "./../../pages/personal/personal";
// import { LoginPage } from '../pages/login /login';
import { LoginPage } from "./.././../pages/login/login";

@Injectable()
export class BackButtonProvider {
  ionicApp: any;
  heckPage: boolean;
  //控制硬件返回按钮是否触发，默认false
  backButtonPressed: boolean = false;
  // backButtonToRoot: boolean = false;

  //构造函数 依赖注入
  constructor(public platform: Platform,
    public appCtrl: App,
    public keyboard: Keyboard,
    // private ionicApp: IonicApp,
    public toastCtrl: ToastController) { }


  // 判断当前页面

  //注册方法
  // 注册返回按键事件
  registerBackButtonAction(tabRef: Tabs,navCtrl, ionicApp): void {
    this.ionicApp = ionicApp;
    this.platform.registerBackButtonAction((): any => {
      //按下返回键时，先关闭键盘
      if (this.keyboard.isOpen()) {
        console.log("关闭键盘")
        //按下返回键时，先关闭键盘
        this.keyboard.close();
        return;
      };
     
      let activePortal = this.ionicApp._modalPortal.getActive() || this.ionicApp._overlayPortal.getActive();
      let loadingPortal = this.ionicApp._loadingPortal.getActive();
      console.log(activePortal);
      console.log(loadingPortal)
      if (activePortal) {
        console.log("关闭模态框")
        //其他的关闭
        if (tabRef == null) {
          console.log(navCtrl.first());
          console.log(navCtrl.length())
          navCtrl.remove(navCtrl.first(), navCtrl.length());
          navCtrl.setRoot(TabsPage);
          // this.ionicApp.clearHistory();
          return;
        }
        activePortal.dismiss().catch(() => {
        });
        activePortal.onDidDismiss(() => {
        });
        return;
      }


      // 获取已激活页面
      let activeNav: NavController = this.appCtrl.getActiveNavs()[0];
      console.log(activeNav)
      //如果可以返回上一页，则执行pop
      if (activeNav.canGoBack()) {
        console.log("关闭nav")
        return activeNav.pop();
      } else {
        this.showExit();
        // if (tabRef._selectHistory[tabRef._selectHistory.length - 1] === tabRef.getByIndex(0).id) {
        //   //执行退出
        //   console.log("无父页面执行退出")

        // } else {
        //   console.log("无根目录返回")
        //   //选择首页第一个的标签
        //   tabRef.select(0);
        // }

      }
      if (loadingPortal) {
        //loading的话，返回键无效
        return;
      }
    }, 101);
  }
  

  private backToRoot(callback): void {

  }
  //退出应用方法
  private showExit(): void {
    //如果为true，退出
    if (this.backButtonPressed) {
      this.platform.exitApp();
    } else {
      //第一次按，弹出Toast
      this.toastCtrl.create({
        message: '再按一次退出应用',
        duration: 2000,
        position: 'top'
      }).present();
      //标记为true
      this.backButtonPressed = true;
      //两秒后标记为false，如果退出的话，就不会执行了
      setTimeout(() => this.backButtonPressed = false, 2000);
    }
  }
}
